package com.evola.dbsm.service
{

	import com.evola.dbsm.communication.CommandObject;
	import com.evola.dbsm.communication.CommandObjectStatus;
	import com.evola.dbsm.communication.TransferObject;
	import com.evola.dbsm.utils.Settings;
	
	import mx.collections.IList;
	import mx.controls.Alert;
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.AMFChannel;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;

	public class RemoteService
	{
		private var remoteObject:mx.rpc.remoting.RemoteObject;

		private var amfChannel:String=Settings.AMF_URL;

		private var remoteServiceName:String="singleEntryPointService";

		private var onResult:Function;
		private var onFault:Function;

		public function RemoteService()
		{
			initializeRemoteObject();
		}

		private function initializeRemoteObject():void
		{
			var cs:ChannelSet=new ChannelSet();
			cs.addChannel(new AMFChannel("my-amf", amfChannel));
			remoteObject=new RemoteObject(remoteServiceName);
			remoteObject.channelSet=cs;

		}

		public function executeRemoteMethod(onResult:Function, onFault:Function, commands:IList):void
		{
			this.onResult=onResult;
			this.onFault=onFault;
			var transferObject:TransferObject=new TransferObject();
			transferObject.setCommands(commands);

			remoteObject.addEventListener(ResultEvent.RESULT, onResultHandler)
			remoteObject.addEventListener(FaultEvent.FAULT, onFaultHandler)
			remoteObject.executeCommand(transferObject);
		}
		
		public function executeTest(input : String, onResult:Function, onFault:Function):void
		{
			this.onResult=onResult;
			this.onFault=onFault;			
			
			remoteObject.addEventListener(ResultEvent.RESULT, onResultHandler)
			remoteObject.addEventListener(FaultEvent.FAULT, onFaultHandler)
			remoteObject.test(input);
		} 

		private function onResultHandler(event:ResultEvent):void
		{

			var transferObject:TransferObject=TransferObject(event.result);
			var commands:IList=transferObject.commands;
			for (var i:int=0; i < commands.length; i++)
			{
				var co:CommandObject=CommandObject(commands.getItemAt(i));
				if (co.status == CommandObjectStatus.OK)
				{
					var serviceResult:ServiceResult=new ServiceResult();
					serviceResult.data=co.data;
					serviceResult.commandName=co.commandName;
					onResult(serviceResult);
				}
				else if (co.status == CommandObjectStatus.FAULT)
				{
					if (onFault == null)
					{
						var message : String = co.data ? co.data.toString() : "/";
						Alert.show(message);
					}
					else
					{
						onFault(co.data);
					}
				}
			}
		}

		private function onFaultHandler(event:FaultEvent):void
		{
			if (onFault == null)
			{
				Alert.show(event.fault.toString());
			}
			else
			{
				onFault(event.fault);
			}
		}

		public function reconnect():void
		{
			initializeRemoteObject();
		}

		public function disconnect():void
		{
			remoteObject.disconnect();
		}

	}
}